package com.shameit.goodqrinfomanager.source.http;

import com.example.mylibrary.Optional;

import org.reactivestreams.Publisher;

import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.Observable;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

public class RxScheduler {
    /**
     * 统一线程处理
     *
     * @param <T> 指定的泛型类型
     * @return FlowableTransformer
     */
    public static <T> FlowableTransformer<T, T> Flo_io_main() {
        return new FlowableTransformer<T, T>() {
            @Override
            public Publisher<T> apply(@NonNull Flowable<T> upstream) {
                return upstream.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    /**
     * 统一线程处理
     *
     * @param <T> 指定的泛型类型
     * @return ObservableTransformer
     */
    public static <T> ObservableTransformer<T, T> Obs_io_main() {
        return upstream -> upstream.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());

    }

//    public static <T extends BaseModel> ObservableTransformer<BaseResponse<T>, T> handle_result() {
//        return new ObservableTransformer<BaseResponse<T>, T>() {
//            @Override
//            public ObservableSource<T> apply(Observable<BaseResponse<T>> upstream) {
//                return upstream
//                        .flatMap(new Function<BaseResponse<T>, ObservableSource<T>>() {
//                                     @Override
//                                     public ObservableSource<T> apply(@NonNull BaseResponse<T> result) throws Exception {
//                                         LogUtils.d("消息体" + new Gson().toJson(result));
//                                         if (BaseResponse.Success == result.getCode()) {
//                                             return Observable.just(result.getData());
//                                         } else if (result.getCode() == BaseResponse.SIGN_OUT) {
//                                             // 发送请求失败的信息
//                                             LogUtils.d("Token 失效");
//                                             return Observable.error(new Exception(result.getMsg()));
//                                         } else {
//                                             return Observable.error(new Exception(result.getMsg()));
//                                         }
//                                     }
//                                 }
//                        );
//            }
//        };
//    }

//    public static <T extends BaseModel> ObservableTransformer<BaseResponse<T>, Optional<T>> handle_nullableresult() {
//        return upstream -> upstream
//                .flatMap(new Function<BaseResponse<T>, ObservableSource<Optional<T>>>() {
//                             @Override
//                             public ObservableSource<Optional<T>> apply(@NonNull BaseResponse<T> result) throws Exception {
//                                 LogUtils.d("nullable 消息体" + new Gson().toJson(result));
//                                 if (result.getCode() == BaseResponse.Success) {
//                                     // result.transform() 就是将返回结果进行包装
//                                     return createHttpData(result.transform());
//                                 } else if (result.getCode() == BaseResponse.SIGN_OUT) {
//                                     // 登录失效逻辑处理
//                                     // ...
//                                     LogUtils.d("Token 失效");
//                                     return Observable.error(new Exception(result.getMsg()));
//                                 } else {
//                                     // 发送请求失败的信息
//                                     String msg = result.getMsg();
//                                     return Observable.error(new Exception(msg));
//                                 }
//                             }
//                         }
//                );
//    }


    public static <T> Observable<Optional<T>> createHttpData(Optional<T> t) {
        return Observable.create(e -> {
            try {
                e.onNext(t);
                e.onComplete();
            } catch (Exception exc) {
                e.onError(exc);
            }
        });
    }
}
